﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.DreamTour.BLL.IService;
using ZhouYu.DreamTour.DAL.IRepository;
using ZhouYu.DreamTour.Model;
using ZhouYu.DreamTour.ViewModel;
using ZhouYu.DreamTour.WinService;
using ZhouYu.DreamTour.WinService.AuditPassSMS;

namespace ZhouYu.DreamTour.WinService.Job
{
    public class ExamineJob : IJob
    {
        private IUserRepository _userRepository;
        public ExamineJob()
        {
            _userRepository = AutofacUtil.GetFromFac<IUserRepository>();
        }


        public async Task Execute(IJobExecutionContext context)
        {
            string str = "";
            ResponseResult responseResult = new ResponseResult();
            //查询获取审核未通过用户的信息
            List<GetCashUser> getCashUsers = _userRepository.QueryExamine();
            List<CashInfo> cashInfos = _userRepository.QueryExamineCashId();
            //调用修改审核通过方法
            responseResult.Success = _userRepository.AuditPass()>0;
            //判断是否成功
            try
            {
                if (responseResult.Success)
                {
                    //循环调用减少冻结金额方法
                    for (int i = 0; i < cashInfos.Count; i++)
                    {
                        responseResult.Success = _userRepository.ReduceCashFreeze(cashInfos[i]) > 0;
                        if (!responseResult.Success) 
                        {
                            break;
                        }
                    }
                    //判断是否成功
                    if (responseResult.Success)
                    {
                        TenXunYunSmSForAuditPass tenXunYunSmSForAudit = new TenXunYunSmSForAuditPass();
                        //循环短信通知审核通过用户
                        for (int i = 0; i < getCashUsers.Count; i++)
                        {
                            str = getCashUsers[i].CashOut_BankNum;
                            str = str.Substring(str.Length - 4, 4);
                            File.AppendAllText("D:\\Ex.txt", getCashUsers[i].CashOut_Money.ToString() + getCashUsers[i].CashOut_From + str + getCashUsers[i].User_NickName + getCashUsers[i].User_Telphone);
                            //发送短信
                            tenXunYunSmSForAudit.SetSMS(getCashUsers[i].User_Telphone.ToString(), getCashUsers[i].User_NickName, getCashUsers[i].CashOut_Money, str, getCashUsers[i].CashOut_From);
                        }
                    } 
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText("D:\\Ex.txt", ex.ToString());
                throw;
            }
            //审核操作
            await Task.Delay(1);
        }
    }
}
